package javasea.proto.cmdBatch;


import com.google.protobuf.MessageOrBuilder;
import com.google.protobuf.TextFormat;
import javasea.proto.cmdBatch.entity.CmdBatch;
import javasea.utils.ProtoBeanUtils;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * protobuf学习
 *
  * @Description
 * func main() {
 *     str := "你好a"
 *
 *     fmt.Printf("%x\n", []byte(str))
 *     fmt.Println("\xe4\xbd\xa0\xe5\xa5\xbd\x61") //用十六进制表示utf8编码的每个字节
 *
 *     fmt.Printf("%o\n", []byte(str))
 *     fmt.Println("\344\275\240\345\245\275\141") //用八进制表示utf8编码的每个字节
 *
 *     for _, r := range str {
 *         fmt.Printf("%x,", r)
 *     }
 *     fmt.Println("")
 *     fmt.Println("\u4f60\u597d\u0061") //用每个字符的unicode值表示这个字符串
 * }
  * @Author longxiaonan@163.com
  * @Date 1:06 2021/6/6 0006
  **/
public class CmdBatch2Bytes {

	public static void main(String[] args) throws Exception {

		// 序列化
		byte[] bytes = toBytes();
		// 反序列化
		toPlayer(bytes);

	}

	/**
	 * 序列化
	 */
	public static byte[] toBytes() throws Exception {
		//获取一个PBPlayer的构造器
		CmdBatchModule.CmdBatch.Builder builder = CmdBatchModule.CmdBatch.newBuilder();
		//设置数据
		List<CmdBatchModule.Command> cmdList = new ArrayList<>();
		// 设置中文的时候有问题，该怎么办呢？
		CmdBatchModule.Command c1 = CmdBatchModule.Command.newBuilder().setBatchId("我").setSn(1).build();
		CmdBatchModule.Command c2 = CmdBatchModule.Command.newBuilder().setBatchId("是").setSn(2).build();
		CmdBatchModule.PrintParam printParam = CmdBatchModule.PrintParam.newBuilder().setCodeType(CmdBatchModule.PrintParam.CodeType.barCode).build();
		CmdBatchModule.Command c3 = CmdBatchModule.Command.newBuilder().setBatchId("11111").setSn(3).setParam(printParam).setSn(2).build();
		cmdList.add(c1);
		cmdList.add(c2);
		cmdList.add(c3);
		builder.setBatchId("100").setNioClientCode("kb-test-001").addAllCommand(cmdList);
		//构造出对象
		CmdBatchModule.CmdBatch cmdBatch = builder.build();

		// proto对象 ==》 java对象
		CmdBatch cmdBatchPojo = ProtoBeanUtils.toPojoBean(CmdBatch.class, cmdBatch);
		System.out.println("cmdBatchPojo>>>>"+cmdBatchPojo);

		// java对象 ==》proto对象
		CmdBatchModule.CmdBatch.Builder newBuilder = CmdBatchModule.CmdBatch.newBuilder();
		ProtoBeanUtils.toProtoBean(newBuilder, cmdBatchPojo);
		CmdBatchModule.CmdBatch cmdBatchProto = newBuilder.build();
		System.out.println("cmdBatchProto>>>>"+cmdBatchProto);

		//序列化成字节数组
		byte[] byteArray = cmdBatch.toByteArray();

		// 打印出 字节 长度，可以看到 protobuf 转换后的字节数很少
		System.out.println(Arrays.toString(byteArray));

		return byteArray;
	}

	/**
	 * 反序列化
	 * @param bs
	 * @throws Exception
	 */
	public static void toPlayer(byte[] bs) throws Exception{

		CmdBatchModule.CmdBatch cmdBatch = CmdBatchModule.CmdBatch.parseFrom(bs);

		// 如果是batchId是中文，则输出乱码
//		System.out.println("batchId:" + cmdBatch.getBatchId());
		System.out.println("batchId:" + cmdBatch.getBatchId());
		 System.out.println("nioClientCode:" + cmdBatch.getNioClientCode());
		 System.out.println("paramList:" + cmdBatch.getCommandList());

		// 可以正常输出中文， 但是无法对属性单独取值
		System.out.println(">>>>>>>>>>>>>>>>>>"+TextFormat.printToUnicodeString(cmdBatch));

		// 再转换一遍还是乱码
		CmdBatchModule.CmdBatch parse = TextFormat.parse(TextFormat.printToUnicodeString(cmdBatch), CmdBatchModule.CmdBatch.class);
		System.out.println("batchId:" + parse.getBatchId().getBytes());
		System.out.println("nioClientCode:" + parse.getNioClientCode());
		System.out.println("paramList:" + parse.getCommandList());

		// 这种方式可以转换汉字，不是乱码，中文乱码问题 解决
		String decode = URLDecoder.decode(cmdBatch.getCommandList().get(2).getBatchId(), "utf-8");
		System.out.println(decode);
	}
}
